<?php
class panel_data extends data 
{
	public function __construct()
	{
		parent::__construct();
		
	}
	
	public function getPgList()
	{
		$this->query("SELECT p.pgid as id, p.pgname, p.pgmain, c.classname FROM pg p left outer join classes c on (p.class=c.classid) WHERE userid={$_SESSION['userid']}");
		if($this->num_rows())
			return $this->fetch_all_row();
		else 
			return null;
	}
	
	public function getPgRoleName($pgid, $mainrole=0)
	{
		$this->query("SELECT r.rolename FROM pgrole p left outer join role r on (p.roleid=r.roleid) WHERE pgid=$pgid AND main=$mainrole");
		list($rolename) = $this->fetch_row();
		if($rolename)
			return $rolename;
		else
			return parent::getRoleNameNull();
	}
	
	public function getPgRoleID($pgid, $mainrole=0)
	{
		$this->query("SELECT roleid FROM pgrole WHERE pgid=$pgid AND main=$mainrole");
		list($roleid) = $this->fetch_row();
		if($roleid)
			return $roleid;
		else
			return parent::getRoleIDNull();
	}
	
	public function mainexists()
	{
		$this->query("SELECT pgname FROM pg WHERE userid = '{$_SESSION['userid']}' and pgmain = 1");
		if($this->num_rows())
		{
			list($pgname) = $this->fetch_row();
			return $pgname;
		}
		else 
			return null;
	}
	
	public function setPg()
	{
		if(isset($_POST['pgid']) and $_POST['pgid'] != null ){
			if(!$this->query("UPDATE pg SET pgname = '{$_POST['pgname']}', userid = '{$_SESSION['userid']}', pgmain = '{$_POST['pgmain']}',  class = '{$_POST['class']}' WHERE pgid = '{$_POST['pgid']}'"))
				return $this->error;
			else
			{
				if(!$this->query("DELETE FROM pgrole WHERE pgid = '{$_POST['pgid']}'"))
					return $this->error;
				else{
					if($this->query("INSERT INTO pgrole SET roleid={$_POST['mainrole']}, pgid={$_POST['pgid']}, main=1")){
						if($_POST['secondrole'] != parent::getRoleIDNull()){
							if(!$this->query("INSERT INTO pgrole SET roleid={$_POST['secondrole']}, pgid={$_POST['pgid']}"))
								return $this->error;
						}
					}
					else
						return $this->error;
				}
			}
		}
		else {
			if(!$this->query("INSERT INTO pg SET pgname = '{$_POST['pgname']}', userid = '{$_SESSION['userid']}', class = '{$_POST['class']}'"))
				return $this->error;
			else
			{
				$pgid = $this->insert_id;
				if($this->query("INSERT INTO pgrole SET roleid={$_POST['mainrole']}, pgid=$pgid, main=1")){
					if($_POST['secondrole'] != parent::getRoleIDNull()){
						if(!$this->query("INSERT INTO pgrole SET roleid={$_POST['secondrole']}, pgid=$pgid"))
							return $this->error;
					}
				}
				else
					return $this->error;
			}
		}
	}
	
	public function deletePg()
	{
		if(!$this->query("DELETE FROM pg WHERE pgid = '{$_POST['pgid']}'"))
			return $this->error;
		else{
			if(!$this->query("DELETE FROM pgrole WHERE pgid = '{$_POST['pgid']}'"))
				return $this->error;
		}
			
	}
	
	public function getPg()
	{
		$this->query("SELECT * FROM pg WHERE userid = '{$_SESSION['userid']}' and pgid = '{$_REQUEST['pgid']}'");
		$pg = $this->fetch_array();
		$pg['mainrole'] = $this->getPgRoleID($pg['pgid'],1);
		$pg['secondrole'] = $this->getPgRoleID($pg['pgid']);
		
		return $pg;
	}
}
?>